//
//  YKTopicCell.m
//  baisi
//
//  Created by 吴高烽 on 16/10/4.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKTopicCell.h"
#import "YKTopic.h"
#import "YKTopicPictureView.h"
#import "YKTopicVoiceView.h"
#import "YKTopicVedioView.h"
#import "YKComment.h"
#import "YKUser.h"
#import "UIImageView+YKExtension.h"
//#import <UIImageView+WebCache.h>
@interface YKTopicCell()
/** 头像 */
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
/** 昵称 */
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
/** 时间 */
@property (weak, nonatomic) IBOutlet UILabel *createTimeLabel;
/** 顶 */
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
/** 踩 */
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
/** 分享 */
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
/** 评论 */
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
/** 文本内容 */
@property (weak, nonatomic) IBOutlet UILabel *text_Label;
/** 图片帖子中间的内容 */
@property (nonatomic ,weak)YKTopicPictureView *pictureView;
/** 声音帖子中间的内容 */
@property (nonatomic ,weak)YKTopicVoiceView *voiceView;
/** 视频帖子中间的内容 */
@property (nonatomic ,weak)YKTopicVedioView *videoView;
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
@property (weak, nonatomic) IBOutlet UILabel *topCmtContentLabel;

@end
@implementation YKTopicCell
-(YKTopicPictureView *)pictureView{
    if (!_pictureView) {
      YKTopicPictureView  *pictureView = [YKTopicPictureView pictureView];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView ;
    }
    return _pictureView ;
}
-(YKTopicVoiceView *)voiceView{
    if (!_voiceView) {
        YKTopicVoiceView *voiceVie = [YKTopicVoiceView voiceView];
        [self.contentView addSubview:voiceVie];
        _voiceView = voiceVie;
    }
    return _voiceView ;
}
-(YKTopicVedioView *)videoView{
    if (!_videoView) {
        YKTopicVedioView *videoView = [YKTopicVedioView vedioView];
        [self.contentView addSubview:videoView];
        _videoView = videoView ;
    }
    return _videoView ;
}
+(instancetype)cell{
    return  [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
   // YKLog(@"%@",self.topic.profile_image);
    // Configure the view for the selected state
}
-(void)setTopic:(YKTopic *)topic{
    _topic = topic ;
//    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    [self.profileImageView setHeaderImage:topic.profile_image];
    self.createTimeLabel.text = topic.created_at;
    self.nameLabel.text = topic.name ;
    self.text_Label.text = topic.text ;
    [self setupButtonTitle:self.dingButton count:topic.ding placeholder:@"顶"];
    [self setupButtonTitle:self.caiButton count:topic.hate placeholder:@"踩"];
    [self setupButtonTitle:self.shareButton count:topic.repost placeholder:@"分享"];
    [self setupButtonTitle:self.commentButton count:topic.comment placeholder:@"评论"];
    if (topic.type == YKTopicTypePicture) {
        self.pictureView.hidden = NO;
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
        self.pictureView.topic = topic;
        self.pictureView.frame =topic.pictureVF ;
        //[self.contentView addSubview:self.pictureView];
    }else if(topic.type == YKTopicTypeVoice){
        self.videoView.hidden = YES;
        self.pictureView.hidden = YES;
        self.voiceView.hidden = NO;
        self.voiceView.topic = topic ;
        self.voiceView.frame = topic.voiceVF ;
       // [self.pictureView removeFromSuperview];
    }else if (topic.type == YKTopicTypeVideo){
        self.videoView.hidden = NO;
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.topic = topic ;
        self.videoView.frame = topic.videoVF;
    
    }else{
        self.videoView.hidden = YES;
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
    }
    // 处理热门评论
    YKComment *cmt = [topic.top_cmt firstObject];
    if (cmt) {
        self.topCmtView.hidden = NO ;
        self.topCmtContentLabel.text = [NSString stringWithFormat:@"%@ : %@",cmt.user.username,cmt.content];
    }else{
        self.topCmtView.hidden = YES ;
    }
}
-(void)setupButtonTitle:(UIButton*)btn count:(NSInteger)count placeholder:(NSString*)title{
    if (count>10000) {
        title = [NSString stringWithFormat:@"%.1f万",count/10000.0];
    }else if (count>0){
        title = [NSString stringWithFormat:@"%zd",count];
    }
    [btn setTitle:title forState:UIControlStateNormal];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    UIImageView *bgView = [[UIImageView alloc] init];
    bgView.image = [UIImage imageNamed:@"mainCellBackground"];
    self.backgroundView = bgView;
}


@end
